package com.xiaochi.subject.application.controller;/**
 * @author zqx
 * @since 2024/7/20
 */

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.base.Preconditions;
import com.xiaochi.subject.application.convert.SubjectAnswerDTOConverter;
import com.xiaochi.subject.application.convert.SubjectInfoDTOConverter;
import com.xiaochi.subject.application.dto.SubjectInfoDTO;
import com.xiaochi.subject.common.entity.PageInfo;
import com.xiaochi.subject.common.entity.PageResult;
import com.xiaochi.subject.common.entity.Result;
import com.xiaochi.subject.domain.entity.SubjectAnswerBO;
import com.xiaochi.subject.domain.entity.SubjectInfoBO;
import com.xiaochi.subject.domain.service.SubjectInfoDomainService;
import com.xiaochi.subject.infra.basic.entity.SubjectInfoEs;
import com.xiaochi.subject.infra.basic.service.SubjectCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 刷题controller
 * @author zqx
 * @since
 */
@RestController
@Slf4j
@RequestMapping("/subject")
public class SubjectController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    /**
     * 新增题目
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO ) {
        try {
            //todo 这里加判断是属于日志性能优化   如果不加判断，就算是不打印这个日志，后面的这个对象也会被序列化，从而减低性能
            if(log.isInfoEnabled()){
                log.info("SubjectController.add.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            //参数校验
            Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDTO.getSubjectName()),"题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(),"题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(),"题目分数不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(),"题目类型不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds()),"题目分类id不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds()),"题目标签id不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.SubjectDTOToBo(subjectInfoDTO);
            List<SubjectAnswerBO> subjectAnswerBOS = SubjectAnswerDTOConverter.INSTANCE.SubjectDTOToBOList(subjectInfoDTO.getOptionList());
            subjectInfoBO.setOptionList(subjectAnswerBOS);
            subjectInfoDomainService.add(subjectInfoBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectController.add.error:{}",e.getMessage(),e);
            return Result.fail("新增题目失败");
        }
    }

    /**
     * 查询题目列表
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO ) {
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectController.getSubjectPage.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            //参数校验
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(),"分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(),"标签id不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.SubjectDTOToBo(subjectInfoDTO);
            PageResult<SubjectInfoBO> boPageResult =  subjectInfoDomainService.getSubjectPage(subjectInfoBO);
            return Result.ok(boPageResult);
        } catch (Exception e) {
            log.error("SubjectController.getSubjectPage.error:{}",e.getMessage(),e);
            return Result.fail("查询题目列表失败");
        }
    }

    /**
     * 查询题目信息
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/querySubjectInfo")
    public Result<PageResult<SubjectInfoDTO>> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO ) {
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectController.getSubjectInfo.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            //参数校验
            Preconditions.checkNotNull(subjectInfoDTO.getId(),"题目id不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.SubjectDTOToBo(subjectInfoDTO);
            SubjectInfoBO boResult =  subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDTO infoDTO = SubjectInfoDTOConverter.INSTANCE.SubjectBOToDTO(boResult);
            return Result.ok(infoDTO);
        } catch (Exception e) {
            log.error("SubjectController.getSubjectInfo.error:{}",e.getMessage(),e);
            return Result.fail("查询题目详细失败");
        }
    }

    /**
     * 全文检索
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/getSubjectPageBySearch")
    public Result<PageResult<SubjectInfoEs>> getSubjectPageBySearch(@RequestBody SubjectInfoDTO subjectInfoDTO ) {
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectController.getSubjectPageBySearch.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            //参数校验
            Preconditions.checkArgument(StringUtils.isNotBlank(subjectInfoDTO.getKeyWord()),"关键字不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.SubjectDTOToBo(subjectInfoDTO);
            subjectInfoBO.setPageNo(subjectInfoDTO.getPageNo());
            subjectInfoBO.setPageSize(subjectInfoDTO.getPageSize());
            PageResult<SubjectInfoEs> boPageResult = subjectInfoDomainService.getSubjectPageBySearch(subjectInfoBO);
            return Result.ok(boPageResult);
        } catch (Exception e) {
            log.error("SubjectController.getSubjectPageBySearch.error:{}",e.getMessage(),e);
            return Result.fail("全文检索失败");
        }
    }

    /**
     * 获取贡献榜
     * @return
     */
    @PostMapping("/getContributeList")
    public Result<List<SubjectInfoDTO>> getContributeList() {
        try {
            List<SubjectInfoBO> boList = subjectInfoDomainService.getContributeList();
            List<SubjectInfoDTO> subjectInfoDTOS = SubjectInfoDTOConverter.INSTANCE.SubjectBOToDTOList(boList);
            return Result.ok(subjectInfoDTOS);
        } catch (Exception e) {
            log.error("SubjectController.getSubjectPageBySearch.error:{}",e.getMessage(),e);
            return Result.fail("获取贡献榜失败");
        }
    }


}
